<template>
  <div id="Login">
    <div class="loginBox">
      <img class="logo" src="../../static/img/logo.png" alt="">
      <el-form :label-position="labelPosition" ref="formLabelAlign" :rules="rules" label-width="80px" :model="formLabelAlign">
        <el-form-item label="登录名" prop="username">
          <el-input v-model="formLabelAlign.username" @keyup.enter.native="submitForm('formLabelAlign')"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="formLabelAlign.password" @keyup.enter.native="submitForm('formLabelAlign')"></el-input>
        </el-form-item>
        <el-form-item>
        <el-button type="primary" @click="submitForm('formLabelAlign')">登录</el-button>
          <el-button @click="resetForm('formLabelAlign')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        labelPosition: 'right',
        formLabelAlign: {
          username: '',
          password: ''
        },
        rules: {
          username: [
            { required: true, message: '请输入登录名', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ]
        }
      };
    },
    name: 'Login',
    components: {},
    computed: {},
    beforeCreated() {},
    created() {},
    mounted() {},
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            //alert('submit!');
            var self = this;
            self.
            postRequest("/loginUser", {
              username:self.formLabelAlign.username,
              password:self.formLabelAlign.password
            }).then(
              res => {      
                console.log(res)
                if(res.data.code == 1 ){            
                  this.$message({
                      message: res.data.message,
                      type: 'success'
                  });
                  self.$router.push('/index');//登录成功跳转到日报列表
                }else if(res.data.code == 2 ){
                  this.$message({
                      message: res.data.message,
                      type: 'error'
                  });
                }else if(res.data.code == 3 ){
                  this.$message({
                      message: res.data.message,
                      type: 'error'
                  });
                }   
              }
            );
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  };
</script>

<style scoped>
  html,body {height: 100%;width: 100%;}
  .loginBox {width: 30%;position: fixed;left: 50%;margin-left: -15%;margin-top: 220px;}
  .logo {margin-bottom: 30px;}
</style>





